﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebNext.UI.Utils;
using WebNext.Domain.Entities;
using WebNext.UI.Properties;

namespace WebNext.UI.Models.Admin
{
	 public class AuthorInfoViewModel
	 {
		 public string Name
		 {
			 get;
			 private set;
		 }

		 public string Description
		 {
			 get;
			 private set;
		 }

		 public string SocialNetwork
		 {
			 get;
			 private set;
		 }

		 public string CustomScript
		 {
			 get;
			 private set;
		 }

		 public string ImageUri
		 {
			 get;
			 private set;
		 }

		 public bool IsNameValid
		 {
			 get;
			 private set;
		 }

		 public bool IsDescriptionValid
		 {
			 get;
			 private set;
		 }

		 public bool IsSocialNetworkValid
		 {
			 get;
			 private set;
		 }

		 public bool IsCustomScriptValid
		 {
			 get;
			 private set;
		 }

		 public bool IsImageUriValid
		 {
			 get;
			 private set;
		 }

		 public AuthorInfoViewModel()
		 {
			 this.IsNameValid = true;
			 this.IsDescriptionValid = true;
			 this.IsSocialNetworkValid = true;
			 this.IsCustomScriptValid = true;
			 this.IsImageUriValid = true;
		 }

		 public AuthorInfoViewModel(AuthorInfo authorInfo) : this()
		 {
			 this.Name = authorInfo.Name;
			 this.Description = authorInfo.Description;
			 this.SocialNetwork = authorInfo.SocialNetwork;
			 this.CustomScript = authorInfo.CustomScript;
			 this.ImageUri = authorInfo.ImageUri;
		 }

		 public AuthorInfoViewModel(string name, string description, string socialNetwork, string customScript, string imageUri) : this()
		 {
			 this.Name = name;
			 this.Description = description;
			 this.SocialNetwork = socialNetwork;
			 this.CustomScript = customScript;
			 this.ImageUri = imageUri;
		 }

		 public ValidationResult Validate()
		 {
			 ValidationResult result = new ValidationResult();
			 if (string.IsNullOrEmpty(this.Name))
			 {
				 result.AddError(Resources.AuthorNameCannotBeEmpty);
				 this.IsNameValid = false;
			 }
			 else if (this.Name.Length > AuthorInfo.MaxNameLength)
			 {
				 result.AddError(Resources.AuthorNameTooLong, AuthorInfo.MaxNameLength);
				 this.IsNameValid = false;
			 }

			 if (this.Description != null && this.Description.Length > AuthorInfo.MaxDescriptionLength)
			 {
				 result.AddError(Resources.AuthorDescriptionTooLong, AuthorInfo.MaxDescriptionLength);
				 this.IsDescriptionValid = false;
			 }

			 if (this.SocialNetwork != null && this.SocialNetwork.Length > AuthorInfo.MaxSocialNetworkLength)
			 {
				 result.AddError(Resources.SocialAddressTooLong, AuthorInfo.MaxSocialNetworkLength);
				 this.IsSocialNetworkValid = false;
			 }

			 if (this.CustomScript != null && this.CustomScript.Length > AuthorInfo.MaxCustomScriptLength)
			 {
				 result.AddError(Resources.CustomScriptTooLong, AuthorInfo.MaxCustomScriptLength);
				 this.IsCustomScriptValid = false;
			 }

			 if (this.ImageUri != null && this.ImageUri.Length > AuthorInfo.MaxImageUriLength)
			 {
				 result.AddError(Resources.AuthorImageUriTooLong, AuthorInfo.MaxImageUriLength);
				 this.IsImageUriValid = false;
			 }

			 return result;
		 }
	 }
}